﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SportManager
{
    public partial class ExercisesListDlg : Form
    {
        private List<ExerciseInfo> m_ExercisesList;
        private List<ExerciseInfo> m_SelectedInfos;

        public ExercisesListDlg(bool isSelectionMode = false, List<ExerciseInfo> selectedInfo = null)
        {
            InitializeComponent();
            PrepareDlg();
            if (isSelectionMode)
            {
                m_SelectedInfos = (selectedInfo == null) ? new List<ExerciseInfo>() : selectedInfo;
                treeView1.CheckBoxes = true;
            }
        }

        private void SelectNodes()
        {
            if (m_SelectedInfos == null || m_SelectedInfos.Count == 0)
            {
                return;
            }

            foreach (ExerciseInfo info in m_SelectedInfos)
            {
                foreach (TreeNode node in treeView1.Nodes)
                {
                    foreach (TreeNode subNode in node.Nodes)
                    {
                        if (subNode.Text == info.Name)
                        {
                            subNode.Checked = true;
                            break;
                        }
                    }
                }
            }
        }

        public List<ExerciseInfo> SelectedInfos
        {
            get
            {
                return m_SelectedInfos;
            }
        }

        private void PrepareDlg()
        {
            label1.Enabled = false;
            labelName.Visible = false;
            labelMuscleGroup.Visible = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            richTextBox1.Enabled = false;
        }

        private void ExercisesList_Load(object sender, EventArgs e)
        {
            dialogHeader1.SetHeaderText("Список упражнений");
            dialogHeader1.SetDescriptionText("Здесь вы можете добавлять и редактировать упражнения");
            dialogHeader1.SetIcon(Resource.stickies_icon32);

            m_ExercisesList = Program.ExercisesKeeper.GetData();
            FillExercisesTree();
            SelectNodes();

            if (m_ExercisesList.Count == 0)
            {
                button2.Enabled = true;
            }
        }

        private void FillExercisesTree()
        {
            int i = 0;
            foreach (ExerciseInfo item in m_ExercisesList)
            {
                if (treeView1.Nodes.IndexOfKey(item.Type.ToString()) < 0)
                {
                    treeView1.Nodes.Add(item.Type.ToString(), item.Type.EnumToString());
                }

                treeView1.Nodes[item.Type.ToString()].Nodes.Add((i++).ToString(), item.Name);
            }

            if (treeView1.Nodes.Count != 0)
            {
                treeView1.Nodes[0].Expand();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (m_SelectedInfos != null)
            {
                m_SelectedInfos.Clear();
            }

            foreach(TreeNode node in treeView1.Nodes)
            {
                foreach(TreeNode subNode in node.Nodes)
                {
                    if (subNode.Checked)
                    {
                        m_SelectedInfos.Add(m_ExercisesList[int.Parse(subNode.Name)]);
                    }
                }
            }

            this.Close();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Parent != null)
            {
                label1.Enabled = true;
                labelMuscleGroup.Visible = true;
                labelName.Visible = true;
                button2.Enabled = false;
                button3.Enabled = true;
                button4.Enabled = true;
                richTextBox1.Enabled = true;
                labelName.Text = treeView1.SelectedNode.Text;
                labelMuscleGroup.Text = m_ExercisesList[int.Parse(treeView1.SelectedNode.Name)].GetLoadsListString();
                richTextBox1.Text = m_ExercisesList[int.Parse(treeView1.SelectedNode.Name)].Description;
            }
            else
            {
                label1.Enabled = false;
                labelName.Visible = false;
                labelMuscleGroup.Visible = false;
                button2.Enabled = true;
                button3.Enabled = false;
                button4.Enabled = false;
                richTextBox1.Enabled = false;
                richTextBox1.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ExerciseType type;
            if (treeView1.SelectedNode == null)
            {
                type = ExerciseType.Power;
            }
            else
            {
                Enum.TryParse<ExerciseType>(treeView1.SelectedNode.Name, out type);
            }

            AddExerciseDlg dlg = new AddExerciseDlg(type);

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                m_ExercisesList.Add(dlg.ResultInfo);
                treeView1.Nodes.Clear();
                FillExercisesTree();                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int indexOfExercise = m_ExercisesList.FindIndex(IsSelectedElement);
            AddExerciseDlg dlg = new AddExerciseDlg(m_ExercisesList[indexOfExercise]);
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                m_ExercisesList[indexOfExercise] = dlg.ResultInfo;
                treeView1.Nodes.Clear();
                FillExercisesTree();
                PrepareDlg();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы уверены, что хотите удалить упражнение безвозвратно?"
                , "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes
              )
            {
                m_ExercisesList.Remove(m_ExercisesList[m_ExercisesList.FindIndex(IsSelectedElement)]);
                treeView1.Nodes.Clear();
                FillExercisesTree();
                PrepareDlg();
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                foreach (TreeNode node in e.Node.Nodes)
                {
                    node.Checked = e.Node.Checked;
                }
            }
        }

        private bool IsSelectedElement(ExerciseInfo item)
        {
            if (item.Name == treeView1.SelectedNode.Text)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
